//
//  editMyResumeAdvantage.swift
//  DingDongZP
//
//  Created by 三秒文化 on 16/5/3.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

import UIKit
import WSProgressHUD

class LDEditMyResumeAdvantage: LDBaseCustomizeVC {
    
    var titleLab:UILabel?
    var textView:UITextView?
    var noticeLab:UILabel?
    var wordOfCountLab:UILabel?
    var textViewStr:String?//网络读取
    var isChange = false
    
    var getTextClosure:((String)->())?
    
//    var preText:String?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let nav = navigationController as? LDRTRootNavVC
        nav?.tapBackNavClosure({ [weak self] in
            
            if self?.isChange == true {
                
                self?.showLDAlertView("信息已编辑,是否退出", sure: {
                    
                    self?.navigationController?.popViewControllerAnimated(true)
                    }, cancel: nil)
            }
            
            })
        
        setDefaultContent()
        setNav()
        requestNetWork()
    }
    
    func requestNetWork() {
        textViewStr = LDUserInfo.sharedInstance.myRemuseInfoDict["mystrengthstext"] as? String
        textView?.text = textViewStr
        
        let textLength = String.CountOfWordFromString(textView!.text)
        let strText = "\(textLength)/140"
        wordOfCountLab?.attributedText = getAttrColor(strText)
    }
    
    
    func setDefaultContent() {
        title = "我的优势"
        view.backgroundColor = LDGolbalColor.BgGoundGray
        titleLab = UILabel.setDetailsLab("我的优势", fontSize: 14, titleColor: LDFontColor.LightBlack, Alignment: .Left)
        
        textView = UITextView()
        textView?.backgroundColor = LDFontColor.White
        textView?.textColor = LDFontColor.DeepGray
        textView?.font = LDFont.Font14
        
        textView?.rac_textSignal().subscribeNext({ [weak self] (obj) in
            
        
            let wordstr = obj as? String ?? ""
            
            if self?.textViewStr == wordstr {
                
                self?.isChange = false
            } else {
                
                self?.isChange = true
            }
            
            
            var wordlength = 0;
            
            if wordstr.characters.count > 140 {
                
                let str = NSString(string: wordstr)
                let range = NSRange.init(location: 0, length: 140)
                
                let rangstr = str.substringWithRange(range)
                
                wordlength = 140;
                self?.textView!.text = rangstr
                self?.showLDAlertView("字数超过了140")
                
            } else {
                
                 wordlength = wordstr.characters.count;
            }
            
            let strText = "\(wordlength)/140"
            self?.wordOfCountLab?.attributedText = self?.getAttrColor(strText)
            
        })
        
        
        noticeLab = UILabel.setDetailsLab("看看别人怎么写", fontSize: 14, titleColor: LDFontColor.Green, Alignment: .Left)
        
        noticeLab?.hidden = true
        wordOfCountLab = UILabel.setDetailsLab("", fontSize: 14, titleColor: LDFontColor.Green, Alignment: .Right)
        
        view.addSubview(titleLab!)
        view.addSubview(textView!)
        view.addSubview(noticeLab!)
        view.addSubview(wordOfCountLab!)
        
        titleLab?.snp_makeConstraints(closure: { (make) in
            make.left.top.equalTo(LDMargin_V*3)
            make.height.equalTo(20)
        })
        textView?.snp_makeConstraints(closure: { (make) in
            make.left.equalTo(LDMargin_V*3)
            make.right.equalTo(-LDMargin_V*3)
            make.top.equalTo((titleLab?.snp_bottom)!).offset(LDMargin_V*2)
            make.height.equalTo(200)
        })
        noticeLab?.snp_makeConstraints(closure: { (make) in
            make.left.equalTo(LDMargin_V*3)
            make.top.equalTo((textView?.snp_bottom)!).offset(LDMargin_V*2)
            make.height.equalTo(20)
            
        })
        wordOfCountLab?.snp_makeConstraints(closure: { (make) in
            make.right.equalTo(-LDMargin_V*3)
            make.top.equalTo(noticeLab!)
            make.height.equalTo(20)
        })
    }
    
    func setNav() {
        let rightBar = UIBarButtonItem.init(image: UIImage.init(named: "common_nav_ok"), style: UIBarButtonItemStyle.Done, target: self, action: #selector(sendAdvantageInfo))
        navigationItem.rightBarButtonItem = rightBar
    }
    
    func sendAdvantageInfo() {
        textView?.endEditing(true)
        let str = String.init(format: "%@", (textView?.text)!)
        if getTextClosure != nil {
            getTextClosure!(str)
        }
        
        LDUserInfoNet.sharedInstance.setMyHeightLightText(str, success: {[weak self] (data) in
            
            LDShowHUB.shareInstance.showSucc(data!)
            LDUserInfo.sharedInstance.updataJobFinderMyResume(nil, failure: nil)
            
            self?.navigationController?.popViewControllerAnimated(true)
        }) { (error) in
            LDShowHUB.shareInstance.showErr(error! as! String)
        }
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        textView?.endEditing(true)
    }
}

extension LDEditMyResumeAdvantage:UITextViewDelegate {
//
//    func textViewDidChange(textView: UITextView) {
//        
//        isChange = true
//        
//        let textLength = String.CountOfWordFromString(textView.text)
//        let strText = "\(textLength)/140"
//        wordOfCountLab?.attributedText = getAttrColor(strText)
//    }
//
    func getAttrColor(str:String) -> NSMutableAttributedString{
        
        let strText = str
        let attr = NSMutableAttributedString.init(string: strText)
        
        let edit = strText.componentsSeparatedByString("/").first
        let range = NSRange.init(location: 0, length: (edit?.characters.count)!)
        
        attr.addAttributes([NSForegroundColorAttributeName:LDFontColor.Green], range: range)
        return attr
    }
}
